// 页面路径：store/index.js
import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex); //vue的插件机制

//Vuex.Store 构造器选项
const store = new Vuex.Store({
  state: {
    //存放状态
    shopCartList: uni.getStorageSync("shopCart") || [],
    token: "",
  },
  mutations: {
    setShopCart(state, value) {
      //存缓存
      uni.setStorageSync("shopCart", value);
      state.shopCartList = value;
    },
    setToken(state, token){
      state.token = token
    }
  },
  actions: {
    async getToken(store) {
      //获取用户信息
      const { encryptedData, rawData, iv, signature } = await wx.getUserProfile(
        {
          desc: "为用户提供更好的体验",
        }
      );
      //登录
      const { code } = await wx.login();
      // console.log(code);
      //请求后台返回token
      const tokenParams = {
        encryptedData,
        rawData,
        iv,
        signature,
        code,
      };
      const res3 = await uni.$u.post("/users/wxlogin", tokenParams);
      const token = res3.message.token;
      store.commit('setToken',token)
      // console.log(token);
    },
  },
});
export default store;
